package edu.univercidade.tcc.sap.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.univercidade.tcc.sap.client.controller.CategoriesMenuCommand;
import edu.univercidade.tcc.sap.client.controller.GroupMenuCommand;
import edu.univercidade.tcc.sap.client.controller.ProjectsMenuCommand;
import edu.univercidade.tcc.sap.client.controller.StatusMenuCommand;
import edu.univercidade.tcc.sap.client.controller.TrackerMenuCommand;
import edu.univercidade.tcc.sap.client.controller.UserMenuCommand;
import edu.univercidade.tcc.sap.client.widget.LoginDialog;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * 
 * Administração Papéis e Permissões Workflow Tarefas Nova Tarefa
 * Membros Categorias
 */
public class Home implements EntryPoint {

	private final UserServiceAsync userService = GWT.create(UserService.class);
	private final Messages messages = GWT.create(Messages.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final RootPanel rootPanel = RootPanel.get();

		ScrollPanel scrollPanel = new ScrollPanel();

		VerticalPanel dockPanel = new VerticalPanel();
		rootPanel.add(dockPanel, 0, 0);
		dockPanel.setStyleName("mainDockPanel");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		dockPanel.add(horizontalPanel);

		MenuBar menuBar = new MenuBar(false);
		horizontalPanel.add(menuBar);

		MenuBar projectsMenuBar = new MenuBar(true);
		MenuItem mntmProjects = new MenuItem(messages.menuProjects(), false,
				projectsMenuBar);
		menuBar.addItem(mntmProjects);

		MenuBar adminstrationMenuBar = new MenuBar(true);
		MenuItem mntmAdministration = new MenuItem(
				messages.menuAdministration(), false, adminstrationMenuBar);

		MenuItem mntmProject = new MenuItem(messages.menuProjects(),
				false, new ProjectsMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmProject);
		
		MenuItem mntmUsers = new MenuItem(messages.menuUsers(), false,
				new UserMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmUsers);

		MenuItem mntmGroups = new MenuItem(messages.menuGroups(), false,
				new GroupMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmGroups);

		MenuItem mntmTrackers = new MenuItem(messages.menuTrackers(), false,
				new TrackerMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmTrackers);

		MenuItem mntmStatus = new MenuItem(messages.menuStatus(), false,
				new StatusMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmStatus);

		MenuItem mntmCategories = new MenuItem(messages.menuCategories(),
				false, new CategoriesMenuCommand(scrollPanel));
		adminstrationMenuBar.addItem(mntmCategories);

		menuBar.addItem(mntmAdministration);

		menuBar.addSeparator();
		
		dockPanel.add(scrollPanel);

		// TODO Melhorar esse acoplamento entre o LoginDialog e MenuBar

		authenticateUser(menuBar);

		LoginDialog lp = new LoginDialog();
		lp.center();
		lp.show();
	}

	private void authenticateUser(final MenuBar menuBar) {
		AsyncCallback<String> userCallback = new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable arg0) {
				userService.loggedUser(this);
			}

			@Override
			public void onSuccess(final String arg0) {
				menuBar.addItem(new MenuItem(SafeHtmlUtils.fromString(arg0)));
			}
		};
		userService.loggedUser(userCallback);
	}

}
